﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;
using System.Configuration;
using System.Data.SqlClient;
using ColorApplication.BusinessLayer;
using ColorApplication.Constants;
using ColorApplication.Helper;
using System.Data;

namespace ColorApplication.color
{
    public partial class Reports : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["id"] != null && Request.QueryString["id"] != "" && Request.QueryString["id"] == ConfigurationManager.AppSettings["AdminID"].ToString())
            {
                Clear();

                if (!IsPostBack)
                {
                    PopulateDataToAttrsFamily();
                }
            }
            else
            {
                Response.Redirect("~/Default.aspx");
            }
        }

        private void PopulateDataToAttrsFamily()
        {
            var atts = ColorSayDaoManager.Instance.GetAttributesFamilyForReport();
            if (atts == null || atts.Count == 0) return;

            var topItem = new ColorSayAttribute();
            topItem.Id = -1;
            topItem.Description = "--- All ---";
            atts.Insert(0, topItem);

            ddlAttrsFamily.DataSource = atts;
            ddlAttrsFamily.DataTextField = "Description";
            ddlAttrsFamily.DataValueField = "Description";
            ddlAttrsFamily.DataBind();
        }

        public void Clear()
        {
            // Clear Error And Success label Every Post Bak
            ErrorLabel.Text = "";
        }

        public override void VerifyRenderingInServerForm(Control control)
        {

        }

        protected string ReportCacheKey
        {
            get
            {
                var guiidValue = Guid.NewGuid().ToString();
                return string.Format(SessionCacheKey.ReportDetails, guiidValue);
            }
        }

        private int GetSelectedOption()
        {
            int result;

            if (!int.TryParse(ddlSelectedOption.SelectedValue, out result))
            {
                result = 0;
            }
            return result;
        }

        protected void OptionChanged(object sender, EventArgs e)
        {
            if (ddlSelectedOption.SelectedValue == "3")
            {
                pnlSelectUrl.Visible = true;

                DateTime? fromdate;
                DateTime? todate;
                if (StartDateTextBox.Text.Trim() == "" && EndDateTextBox.Text.Trim() == "")
                {
                    fromdate = todate = null;
                }
                else
                {
                    fromdate = DateTime.ParseExact(StartDateTextBox.Text, StartDateTextBox_CalendarExtender.Format, null);
                    todate = DateTime.ParseExact(EndDateTextBox.Text, EndDateTextBox_CalendarExtender.Format, null);
                }
                var listUrls = ColorSayDaoManager.Instance.GetAnalysisUrlForReport(fromdate, todate);
                listUrls.Insert(0, "--- All ---");

                ddlSelectUrl.DataSource = listUrls;
                ddlSelectUrl.DataBind();
            }
            else
            {
                pnlSelectUrl.Visible = false;
            }
        }
    }
}